package cn.wolfcode.intercepter;


import cn.wolfcode.domain.Employee;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截器拦截
//用户没有登录，直接网址访问时
//使用拦截器对请求进行拦截请求，判断是否登录了（根据session来判断）
//如果没有登录，则跳转到登录页面
@Component
public class LoginIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拿到登录时，用户记录的session的键值对
        Employee employee  = (Employee)request.getSession().getAttribute("USER_IN_SESSION");
        if (employee == null){  //拿不到，用户不存在
             response.sendRedirect("/login.html");  //跳转回登录页面
            return  false;   //拦截
        }
        return true;  //放行
    }
}
